/**
 * 工程名: 	Quickdisk
 * 文件名: 	UserLoginAction.java
 * 创建人:  	guo.tianxiang
 * 创建时间: 	2014-4-24 下午4:06:46
 * 版权所有：Guo tianxiang 
 * 文件描述: 描述该文件的作用
 * -----------------------------变更记录 ----------------------------- 
 * 日期        		变更人      		版本号  		变更描述  
 * ------------------------------------------------------------------  
 * 2014-4-24     guo.tianxiang   	1.0       	first created  
 */
package com.tianxiang.action;

import java.util.ArrayList;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tianxiang.model.Admin;
import com.tianxiang.model.Disk;
import com.tianxiang.model.User;
import com.tianxiang.service.CheckDiskService;
import com.tianxiang.service.LoginService;

/**
 * 验证用户登录的Action
 * 
 * @since Quickdisk 1.0
 * @version 1.0 2014-4-24
 * @author guo.tianxiang
 */
public class CheckLoginAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String tip;
	private String username;
	private String password;
	private HttpSession session;// 已经登录的用户session

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}
/**
 * 普通用户登录验证
 * @return
 */
	public String login() {
		Object loginuser = ServletActionContext.getRequest().getSession()
				.getAttribute("loginuser");
		if (loginuser != null) {
			System.out.println("loginuser=" + loginuser.toString());
			System.out.println("用户已经登录");
			return SUCCESS;
		}

		ArrayList<Object> loginResultObject = LoginService.checkUserLogin(username, password);
		// 如果为true,登录通过
		if (((String) loginResultObject.get(0)).equals("0")) {
			tip = "没有找到此用户";
			return LOGIN;
		} else if (((String) loginResultObject.get(0)).equals("2")) {
			tip = "密码错误";
			return LOGIN;
		} else if (((String) loginResultObject.get(0)).equals("1")){
			User loginUser = (User) loginResultObject.get(1);
			ActionContext.getContext().getSession().put("loginuser", loginUser);
			return SUCCESS;
		}
		return LOGIN;
	}
	/**
	 * 管理员登录验证
	 * @return
	 */
	public String adminLogin() {
		System.out.println("正在执行LoginAction……");
		
		Object loginadmin = ServletActionContext.getRequest().getSession()
				.getAttribute("loginadmin");
		if (loginadmin != null) {
			//System.out.println("loginuser=" + loginadmin.toString());
			System.out.println("用户已经登录");
			return SUCCESS;
		}
		System.out.println("输入的用户名和密码是："+username+","+password);
		ArrayList<Object> adminLoginResult = LoginService.checkAdminLogin(username, password);
				
		// 如果为true,登录通过
		if (((String) adminLoginResult.get(0)).equals("0")) {
			tip = "没有找到此管理员账号";
			System.out.println("action :"+LOGIN);
			return LOGIN;
		} else if (((String) adminLoginResult.get(0)).equals("2")) {
			tip = "密码错误";
			System.out.println("action:"+LOGIN);
			return LOGIN;
		} else if (((String) adminLoginResult.get(0)).equals("1")){
			Admin loginAdmin = (Admin) adminLoginResult.get(1);
			ActionContext.getContext().getSession().put("loginadmin", loginAdmin);
			System.out.println("action:"+SUCCESS);
			//获取磁盘数据
			ArrayList<Disk>disks=new CheckDiskService().getDiskList();
			System.out.println("磁盘总数："+disks.size());
			ActionContext.getContext().getSession().put("disks", disks);
			return SUCCESS;
		}
		System.out.println("end action:"+LOGIN);
		return LOGIN;
	}
}
